﻿using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interactivity;
using DiagramDesigner.Common.Controls;
using System.Linq;

namespace Behaviour.Selection
{
    public class SelectionBehavior : Behavior<DesignerItem>
    {
        private DesignerCanvas _canvas;

        #region IsSelectedAdorner attached property

        public static readonly DependencyProperty IsSelectedAdornerProperty =
            DependencyProperty.RegisterAttached("IsSelectedAdorner", typeof (bool), typeof (SelectionBehavior),
                                                new PropertyMetadata(default(bool), PropertyChangedCallback));

        private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            bool newValue = (bool) e.NewValue;
            bool oldValue = (bool) e.OldValue;
            if (newValue == oldValue) return;
            DesignerItem item = dependencyObject as DesignerItem;
            if (item == null) return;

            AdornerLayer adornerLayer = null;
            if (newValue)
            {
                adornerLayer = AdornerLayer.GetAdornerLayer(item);
                if (adornerLayer == null) return;
                SelectionAdorner selectionAdorner = new SelectionAdorner(item);
                AdornerLayer.GetAdornerLayer(item).Add(selectionAdorner);
            }
            else
            {
                adornerLayer = AdornerLayer.GetAdornerLayer(item);
                if (adornerLayer == null) return;
                Adorner[] adorners = adornerLayer.GetAdorners(item);
                if (adorners != null)
                {
                    foreach (Adorner adorner in adorners)
                    {
                        if (adorner.GetType() == typeof (SelectionAdorner))
                        {
                            adornerLayer.Remove(adorner);
                            break;
                        }
                    }
                }
            }
        }

        public static void SetIsSelectedAdorner(UIElement element, bool value)
        {
            element.SetValue(IsSelectedAdornerProperty, value);
        }

        public static bool GetIsSelectedAdorner(UIElement element)
        {
            return (bool) element.GetValue(IsSelectedAdornerProperty);
        }

        #endregion


        protected override void OnAttached()
        {
            AssociatedObject.MouseLeftButtonDown += AssociatedObjectOnMouseLeftButtonDown;
        }

        private void AssociatedObjectOnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            bool isSelected = GetIsSelectedAdorner(AssociatedObject);
            SetIsSelectedAdorner(AssociatedObject, !isSelected);
            _canvas = AssociatedObject.Parent as DesignerCanvas;
            if (_canvas == null) return;
            foreach (var child in _canvas.Children.OfType<DesignerItem>())
            {
                if (!child.Equals(sender))
                    SetIsSelectedAdorner(child, false);
            }
        }
    }
}
